<template>
	<!-- 直接生成盘点单 -->
	<view class>
		<form class="login-form">
			<view class="cu-form-group margin-top">
				<view class="title">集团</view>
				<input placeholder="请选择集团" v-model="mainOrder.GROUPNAME" disabled />
			</view>
			<view class="cu-form-group" @tap="activeIndexM(2,'BA_ORGANIZE')">
				<view class="title">组织</view>
				<input placeholder="请选择组织" v-model="mainOrder.ORGNAME" disabled />
			</view>
			<view class="cu-form-group">
				<view class="title">
					位置
					<text class="text-red">*</text>
				</view>
				<!-- <view style="width:100%" v-if="mainOrder.STATIONNAME!=''">
          <text>{{mainOrder.STATIONNAME}}</text>
        </view> -->
				<view class="flexBox">
					<input placeholder="请选择位置" v-model="mainOrder.STATIONNAME " disabled @tap="searchFn('BD_RACK')" />
					<button class="cu-btn bg-green shadow" @tap="openScan('STATION')">扫一扫</button>
				</view>
			</view>

			<view class="cu-form-group">
				<view class="title">盘点人</view>
				<input v-model="mainOrder.INVER" disabled />
			</view>

			<view class="cu-form-group">
				<view class="title">备注</view>
				<input placeholder="请输入备注" v-model="mainOrder.VMEMO" />
			</view>
		</form>

		<view class="btn-area padding">
			<button hover-class="e-hover-btn" class="login-btn bg-blue" type="primary" @click="submitForm"
				:loading="isDisabled" :disabled="isDisabled">下一步</button>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from "vuex";
	import api from "@/common/api.js";
	import utils from "@/common/util.js";
	let _self, activeType;
	export default {
		computed: mapState(["userInfo", "baseUrl"]),
		data() {
			return {
				list: [],
				checkOrder: [],
				activeLineIndex: "",
				mainOrder: {
					BILLNO: "",
					// 返回 PK_ORG(组织id),ORGNAME(组织名称), PK_STORE（仓库id），STORENAME（仓库名称）
					// PK_STATION(货位ID)，STATIONNAME（货位名称）:'',
					PK_GROUP: "",
					GROUPNAME: "",
					PK_ORG: "",
					ORGNAME: "",
					PK_STORE: "",
					STORENAME: "",
					PK_STATION: "",
					STATIONNAME: "", // 货位
					PK_STOREAREA: "", // 货区id
					STOREAREANAME: "", // 货区id
					PK_STORESHELVES: "", // 货架id
					STORESHELVESNAME: "", // 货架id
					PK_STORELAYER: "", // 货层
					STORELAYERNAME: "", // 货层
					VDEF1: "", // 位置id
					BDATAFROM: 1,
					PK_RACK: "",
					RACKNAME: "",
					PK_INVER: "",
					INVER: "",
					VMEMO: "",
					account: "",
					userId: ""
				},
				isDisabled: false,
				LocationList: [],
				PageData: []
			};
		},
		onLoad(p) {
			_self = this;
			this.initSetData();
			this.listenOnce()
			// 已存在的位置信息
			if (p) {
				this.PageData = JSON.parse(decodeURIComponent(p.data || '[]'));
				let LocationInfo = this.PageData[0];
				this.LocationList = LocationInfo ? JSON.parse(LocationInfo.VDEF18) : [];
				this.mainOrder.VDEF1 = LocationInfo ? this.LocationList[0].PK_STATION : '';
				this.mainOrder.STATIONNAME = LocationInfo ? this.LocationList[0].STATIONNAME : '';
			}
		},
		onShow() {

		},

		methods: {
			// 初始化赋值
			initSetData() {
				_self.mainOrder.GROUPNAME = _self.userInfo.GROUPNAME || "";
				_self.mainOrder.PK_GROUP = _self.userInfo.PK_GROUP;
				_self.mainOrder.ORGNAME = _self.userInfo.ORGNAME;
				_self.mainOrder.PK_ORG = _self.userInfo.ORGANIZEID;
				_self.mainOrder.DEPTNAME = _self.userInfo.DEPTNAME;
				_self.mainOrder.PK_DEPT = _self.userInfo.DEPARTMENTID;
				_self.mainOrder.APPLYER = _self.userInfo.Account;
				_self.mainOrder.PK_APPLYER = _self.userInfo.UserId;
				_self.mainOrder.PK_INVER = _self.userInfo.UserId;
				_self.mainOrder.INVER = _self.userInfo.REALNAME;
				_self.mainOrder.userId = _self.userInfo.UserId;
				_self.mainOrder.account = _self.userInfo.Account;
			},
			// 监听 once
			listenOnce() {
				uni.$on("updataPlace", res => {
					// console.log(99,res);
					_self.mainOrder.VDEF1 = res.R + ':' + res.id;
					_self.mainOrder.STATIONNAME = res.name;
					_self.mainOrder.PK_STATION = res.id;
					
				});

				uni.$on("getSelectItem", function(data) {
					console.log(data);
					switch (activeType) {
						case "BA_STOREAREA": // 货区
							_self.mainOrder.STOREAREANAME = data.FullName;
							_self.mainOrder.PK_STOREAREA = data.OrganizeId;
							break;
						case "BA_ORGANIZE":
							_self.mainOrder.ORGNAME = data.FullName;
							_self.mainOrder.PK_ORG = data.OrganizeId;
							break;
						case "BA_DEPARTMENT":
							_self.mainOrder.DEPTNAME = data.FullName;
							_self.mainOrder.PK_DEPT = data.OrganizeId;
							break;
						case "BD_RACK":
							_self.mainOrder.STATIONNAME = data.VNAME;
							_self.mainOrder.PK_STATION = data.BILLNO;
							_self.mainOrder.VDEF1 = data.BILLNO;
							break;
						case "BA_USER":
							_self.mainOrder.APPLYER = data.RealName;
							_self.mainOrder.PK_APPLYER = data.UserId;
							break;
						case "BD_STORE": // 仓库
							_self.mainOrder.PK_STORE = data.BILLNO;
							_self.mainOrder.STORENAME = data.VNAME;
							break;
						default:
							break;
					}
				});
			},

			// 计算盈亏数量
			getPDNun(e, i) {
				let val = e.target.value;
				_self.checkOrder[i].PYKNUM = val - _self.checkOrder[i].KNUM;
			},
			// 查询 手动选择事件
			activeIndexM(i, t) {
				_self.activeLineIndex = i;
				activeType = t;
				let queryParames = "",
					pk_my_org = "";
				if (t == "BA_DEPARTMENT") {
					pk_my_org = this.mainOrder.PK_ORG;
				} else if (t == "BA_ORGANIZE") {
					pk_my_org = this.mainOrder.PK_GROUP;
				}
				uni.navigateTo({
					url: "/pages/public/search?t=" +
						t +
						"&organizeId=" +
						pk_my_org +
						"&queryjson=" +
						queryParames
				});
			},
			searchFn(t) {
				let id = _self.mainOrder.PK_ORG;
				uni.navigateTo({
					url: "/pages/public/ztree?PK_ORG=" + id
				});
			},
			submitForm() {
				let saveData = {
						..._self.mainOrder
					},
					URL = '/api/APPCommon//GetPageList_StockInfo';
				if (!_self.mainOrder.VDEF1) {
					return this.$utils.tips("请选择仓库和位置");
				}
				
				uni.navigateTo({
					url: "mater-list?id=" + _self.mainOrder.STATIONNAME+'&PK_ORG='+ this.mainOrder.PK_ORG+'&type=999'
				});
				return ;
				console.log(saveData);

				this.axios({
					url: URL,
					data: saveData,
					isLoading: 1
				}).then(res => {
					console.log(33, res);
					let {
						rows 
					} = res.Result ;
					let id = rows[0].BILLNO;
					if (!rows.length) {
						return this.$utils.tips("该位置下不存在盘点物资，无需盘点。");
					}
					uni.navigateTo({
						url: "mater-list?id=" + id
					});
					
				}).catch(err => {					
					console.log(44, err);
					return uni.showModal({
						title: '提示',
						content: Message + ',是否继续盘点上次未完成的盘点单？',
						success(res) {
							if (res.confirm) {
								// console.log('继续打印');
								uni.navigateTo({
									url: "mater-list?id=" + Result
								});
							}
						}
					})
				})
			},

			openScan(id, index) {

				let URL = api.apiPublic.ScanCode;
				this.$utils.Scan(URL, data => {
					console.log(data);
					if (!data.PK_STATION) { // 或者不存在就是仓库
						_self.mainOrder.PK_STORE = data.PK_STORE;
						_self.mainOrder.STATIONNAME = data.STORENAME;
						_self.mainOrder.VDEF1 = '1:' + data.PK_STORE
					} else {
						_self.mainOrder.PK_STATION = data.PK_STATION;
						_self.mainOrder.STATIONNAME = data.STATIONNAME;
						_self.mainOrder.VDEF1 = '0:' + data.PK_STATION
					}

				});
			}
		}
	};
</script>

<style scoped>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}

	.flexBox {
		width: 100%;
		display: flex;
		align-items: center;
		/* background-color: #1CBBB4; */
	}

	.mater-item {
		width: 680rpx;
		padding: 20rpx 10rpx;
		margin: 10rpx auto;
		background-color: #ffffff;
		border-radius: 16rpx;
	}

	.bottom-border {
		border-bottom: 1rpx solid #007aff;
		text-align: right;
	}

	.bottom-border {
		text-align: left;
	}

	.bottom-border:focus-within {
		border-bottom: 1rpx solid #007aff;
	}
</style>
